package com.lanou.myapp.comic.themecomic;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lanou.myapp.R;
import com.lanou.myapp.comic.themecomic.contentthemecomic.ContentThemeComicActivity;
import com.lanou.myapp.imageloader.GsonRequest;
import com.lanou.myapp.imageloader.VolleySingleton;

import java.lang.reflect.Type;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/11.
 */
public class ThemeComicFragment extends Fragment {
    private Context context;
    private VolleySingleton queue;
    private ListView listView;
    private ThemeComicAdapter adapter;
    private String url = "http://v2.api.dmzj.com/subject/0/0.json";
    private List<ThemeComic> data;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView = (ListView) getActivity().findViewById(R.id.listView_theme_comic);
        adapter = new ThemeComicAdapter(getActivity());
        queue = VolleySingleton.getInstance();

        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                //返回直接是{}类型的用
                Type type = new TypeToken<List<ThemeComic>>() {
                }.getType();
                data = new ArrayList<>();
                data = gson.fromJson(response, type);

                adapter = new ThemeComicAdapter(getActivity());
                adapter.addData(data);
                listView.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        queue._addRequest(request);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(getActivity(), ContentThemeComicActivity.class);
                        intent.putExtra("key", data.get(position).getId());
//                Log.d("dfdfdf", data.get(position).getId() + "");
                        getActivity().startActivity(intent);
                    }
                });
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_theme_comic, null);
        return view;
    }
}
